<?php

/**
 * This is the model class for table "{{article_comment}}".
 *
 * The followings are the available columns in table '{{article_comment}}':
 * @property string $id
 * @property integer $article_id
 * @property integer $user_id
 * @property string $token
 * @property string $type
 * @property integer $star1
 * @property integer $star2
 * @property integer $star3
 * @property integer $star4
 * @property integer $star5
 * @property integer $star6
 * @property integer $star7
 * @property string $tags
 * @property string $content
 * @property integer $create_time
 */
class ArticleComment extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{article_comment}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('type', 'required'),
			array('article_id, user_id, star1, star2, star3, star4, star5, star6, star7, create_time', 'numerical', 'integerOnly'=>true),
			array('token', 'length', 'max'=>20),
			array('type', 'length', 'max'=>9),
			array('tags', 'length', 'max'=>200),
			array('content', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, article_id, user_id, token, type, star1, star2, star3, star4, star5, star6, star7, tags, content, create_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'article_id' => 'Article',
			'user_id' => 'User',
			'token' => 'Token',
			'type' => 'Type',
			'star1' => 'Star1',
			'star2' => 'Star2',
			'star3' => 'Star3',
			'star4' => 'Star4',
			'star5' => 'Star5',
			'star6' => 'Star6',
			'star7' => 'Star7',
			'tags' => 'Tags',
			'content' => 'Content',
			'create_time' => 'Create Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('article_id',$this->article_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('token',$this->token,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('star1',$this->star1);
		$criteria->compare('star2',$this->star2);
		$criteria->compare('star3',$this->star3);
		$criteria->compare('star4',$this->star4);
		$criteria->compare('star5',$this->star5);
		$criteria->compare('star6',$this->star6);
		$criteria->compare('star7',$this->star7);
		$criteria->compare('tags',$this->tags,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('create_time',$this->create_time);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return ArticleComment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    public function getComment($token,$first,$total){
        $db = Yii::app()->db->createCommand();
        $info = $db
            ->select('ac.*,u.nick_name,u.username')
            ->from('{{article_comment}} ac')
            ->leftJoin('{{user}} u','ac.user_id=u.id')
            ->where('ac.token=:token',array(':token'=>$token))
            ->order('ac.id desc')
            ->limit($total,$first)
            ->queryAll();
        return $info;
    }
    public function getList($token){
        $criteria = new CDbCriteria;
        $criteria->addCondition("token='$token'");
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>10,
            ),
        ));
        return $dataProvider;
    }
}
